import { constantRoutes } from '@/router'
import Layout from '@/layout/index'
import ParentView from '@/components/ParentView';

const permission = {
  state: {
    routes: [],
    addRoutes: []
  },
  mutations: {
    SET_ROUTES: (state, routes) => {
      state.addRoutes = routes
      state.routes = constantRoutes.concat(routes)
    }
  },
  actions: {
    // 生成路由
    GenerateRoutes({ commit }) {
      return new Promise(resolve => {
        const accessedRoutes = filterAsyncRouter(menu.data)
        accessedRoutes.push({ path: '*', redirect: '/404', hidden: true })
        commit('SET_ROUTES', accessedRoutes)
        resolve(accessedRoutes)
      })
    }
  }
}

// 遍历后台传来的路由字符串，转换为组件对象
function filterAsyncRouter(asyncRouterMap) {
  return asyncRouterMap.filter(route => {
    if (route.component) {
      // Layout ParentView 组件特殊处理
      if (route.component === 'Layout') {
        route.component = Layout
      } else if (route.component === 'ParentView') {
        route.component = ParentView
      } else {
        route.component = loadView(route.component)
      }
    }
    if (route.children != null && route.children && route.children.length) {
      route.children = filterAsyncRouter(route.children)
    }
    return true
  })
}

export const loadView = (view) => { // 路由懒加载
  return (resolve) => {
    require([`@/views/${view}`], resolve);
  }
}

export default permission

const menu = {
  "msg": "操作成功",
  "code": 200,
  "data": [
    {
      "name": "Service",
      "path": "/service",
      "hideen": false,
      "redirect": "noRedirect",
      "component": "Layout",
      "alwaysShow": true,
      "meta": {
        "title": "服务管理",
        "icon": "guide",
        "noCache": false
      },
      "children": [
        {
          "name": "Worker",
          "path": "worker",
          "hidden": false,
          "component": "service/worker/index",
          "meta": {
            "title": "工作进程",
            "icon": "server",
            "noCache": false
          }
        },
        {
          "name": "Task",
          "path": "task",
          "hidden": false,
          "component": "service/task/index",
          "meta": {
            "title": "定时任务",
            "icon": "job",
            "noCache": false
          }
        },
        {
          "name": "Deploy",
          "path": "deploy",
          "hidden": false,
          "component": "service/deploy/index",
          "meta": {
            "title": "安装包",
            "icon": "zip",
            "noCache": false
          }
        }
      ]
    },
    {
      "name": "Monitor",
      "path": "/monitor",
      "hideen": false,
      "redirect": "noRedirect",
      "component": "Layout",
      "alwaysShow": true,
      "meta": {
        "title": "监控管理",
        "icon": "monitor",
        "noCache": false
      },
      "children": [
        {
          "name": "Log",
          "path": "log",
          "hidden": false,
          "component": "monitor/log/index",
          "meta": {
            "title": "全部日志",
            "icon": "log",
            "noCache": false
          }
        },
        {
          "name": "jvm",
          "path": "jvm",
          "hidden": false,
          "component": "monitor/jvm/index",
          "meta": {
            "title": "JVM管理",
            "icon": "code",
            "noCache": false
          }
        },
        {
          "name": "HostMonitor",
          "path": "host",
          "hidden": false,
          "component": "monitor/host/index",
          "meta": {
            "title": "主机监控",
            "icon": "druid",
            "noCache": false
          }
        },
        {
          "name": "Alarm",
          "path": "alarm",
          "hidden": false,
          "component": "monitor/alarm/index",
          "meta": {
            "title": "告警管理",
            "icon": "server",
            "noCache": false
          }
        }
      ]
    },
    {
      "name": "System",
      "path": "/system",
      "hideen": false,
      "redirect": "noRedirect",
      "component": "Layout",
      "alwaysShow": true,
      "meta": {
        "title": "系统管理",
        "icon": "system",
        "noCache": false
      },
      "children": [
        {
          "name": "Host",
          "path": "host",
          "hidden": false,
          "component": "system/host/index",
          "meta": {
            "title": "主机管理",
            "icon": "redis",
            "noCache": false
          }
        },
        {
          "name": "Cluster",
          "path": "cluster",
          "hidden": false,
          "component": "system/cluster/index",
          "meta": {
            "title": "管理进程",
            "icon": "tree",
            "noCache": false
          }
        },
        {
          "name": "Menu",
          "path": "menu",
          "hidden": false,
          "component": "system/menu/index",
          "meta": {
            "title": "菜单管理",
            "icon": "tree-table",
            "noCache": false
          }
        }
      ]
    }
  ]
}

